package com.kefu.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.call.service.CallCompanyService;
import com.kefu.call.service.CallGatewayService;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.sys.model.CdrRghistoryTenant;
import com.kefu.sys.model.SysTenant;
import com.kefu.sys.service.CdrRghistoryTenantService;
import com.kefu.sys.service.SysTenantService;
import com.kefu.sys.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags="机构管理")
@RestController
@RequestMapping("sys/tenant")
public class SysTenantController extends AbstractBaseController {
    @Resource
    protected SysTenantService sysTenantService;
    @Resource
    protected CdrRghistoryTenantService cdrRghistoryTenantService;
    @Resource
    protected CallCompanyService callCompanyService;
    @Autowired
    private CallGatewayService callGatewayService;

    @PostMapping("/list")
    @ApiOperation(value="机构列表", notes = "机构列表")
    public Result<Page<SysTenant>> tenantList(@Validated @RequestBody SysTenantSearchVo search) {
        Page<SysTenant> list = sysTenantService.searchLambdaPage(search);
        // 对list进行遍历,获取每个公司的线路名称
        list.getRecords().forEach(sysTenant -> {
            sysTenant.setGateWayName(callGatewayService.findByCompany(sysTenant.getTenantId()));
            sysTenant.setGateWayId(callGatewayService.findIdByCompany(sysTenant.getTenantId()));
            sysTenant.setVersionName(sysTenantService.getVersionName(sysTenant.getTenantId()));
        });

        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/consumeList")
    @ApiOperation(value="账单记录查询", notes = "可用通话时长充值/消费记录查询")
    public Result<Page<CdrRghistoryTenant>> tenantList(@Validated @RequestBody SysTenantConsumptionsVo search) {
        Page<CdrRghistoryTenant> list = cdrRghistoryTenantService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/create")
    @ApiOperation(value="新建机构", notes = "新建机构")
    public Result<String> tenantAdd(@Validated @RequestBody SysTenantInsertVo sysTenantVo) {
        SysTenant sysTenant = sysTenantService.tenantAdd(sysTenantVo);
        callCompanyService.initCompany(sysTenant);
        return ResultGenerator.genSuccessResult(sysTenant.getTenantId());
    }

    @PostMapping("/tenantUpdate")
    @ApiOperation(value="机构修改",notes = "可以修改：租户信息，机器人数量，租户最大权限集")
    public Result<String> tenantUpdate(@Validated @RequestBody SysTenantUpdateBossVo sysTenantVo) {
        sysTenantService.updateByBoss(sysTenantVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/recharge")
    @ApiOperation(value="机构充值",notes = "机构可用通话时长充值")
    public Result<String> recharge(@Validated @RequestBody SysTenantRechargeVo sysTenantVo) {
        sysTenantService.recharge(sysTenantVo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除:{}", delete);

        sysTenantService.deleteByTenantId(delete.getId());
        return ResultGenerator.genSuccessResult();
    }
}
